﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.ServiceModel.DomainServices.Server;
using System.Linq;
using NetBay.Core.BusinessEntities.Versioning;
using NetBay.Core.BusinessEntities.Workflow;

#endregion

namespace NetBay.Core.BusinessEntities
{
    /// <summary>
    /// DATABASE TABLENAME : r_doc
    /// </summary>
    [DataContract]
    public class Document : ReturnObject 
    {
        #region Private members
        /// <summary>
        /// Database identifier (rd_no)
        /// </summary>
        private long m_lIdentifier;

        /// <summary>
        /// Folder relationShip
        /// </summary>
        private Folder m_oFolder;

        /// <summary>
        /// Folder ID (****)
        /// </summary>
        private long m_lFolderId;

        /// <summary>
        /// Application Identifier (rd_iddoc)
        /// </summary>
        private string m_szFolderIdentifier;

        /// <summary>
        /// Image name identifier (rd_idmage)
        /// </summary>
        private string m_szImageName;

        /// <summary>
        /// Rank of item that this document is associate (rd_item)
        /// </summary>
        private string m_szItemRank;

        /// <summary>
        /// Relative path where to find this document image (rd_path)
        /// </summary>
        private string m_szRelativePath;

        /// <summary>
        /// Folder type identifier (rd_td)
        /// </summary>
        private long m_lFolderTypeIdentifier;

        /// <summary>
        /// Rank of this document in the folder (rd_rang)
        /// </summary>
        private int m_iRankInFolder;

        /// <summary>
        /// Note associated to this document (rd_note)
        /// </summary>
        private string m_szNote;

        /// <summary>
        /// Indicate whether the document is deleted (rd_suppr)
        /// </summary>
        private bool m_bDeleted;

        /// <summary>
        /// Indicate the status in the process of fullText (not used alrerady) (rd_fulltext)
        /// </summary>
        private string m_szFullText;

        /// <summary>
        /// Indicate the Author of this document (not valued) (rd_auteur)
        /// </summary>
        private string m_szAuthor;

        /// <summary>
        /// Indicate the credit for the Image's Author (rd_credit_photo)
        /// </summary>
        private string m_szPhotoCredit;

        /// <summary>
        /// Identity courrier of ERP Genesis
        /// </summary>
        private string m_szIdCourrierErp;

        /// <summary>
        /// Identity mesure of ERP Genesis
        /// </summary>
        private string m_szMesureErp;

        /// <summary>
        /// Indicate the duration of video (not valued) (rd_duree_video)
        /// </summary>
        private string m_szVideoLength;

        /// <summary>
        /// Publication Date of image (rd_publie_le)
        /// </summary>
        private DateTime m_oPublicationDate;

        /// <summary>
        /// Modification Date of image (rd_datemod)
        /// </summary>
        private DateTime m_oModificationDate;

        /// <summary>
        /// Identifier of folder (friendly human) (rd_id_dossier)
        /// </summary>
        private int m_iFolderHumanIdentifier;

        /// <summary>
        /// Identifier of image (friendly human) (rd_id)
        /// </summary>
        private int m_iImageHumanIdentifier;

        /// <summary>
        /// Original name file (rd_fichier_origine)
        /// </summary>
        private string m_szOriginalNameFile;
        
        /// <summary>
        /// Folder where the document is stored (only root of path) (rd_pathmultivol)
        /// </summary>
        private string m_szMultiVolumePath;

        /// <summary>
        /// Indicate if a thumbnail exist (rd_imagette)
        /// </summary>
        private bool m_bThumbnail;

        /// <summary>
        /// Creation Date of image (rd_datecreation)
        /// </summary>
        private DateTime m_oCreationDate;

        /// <summary>
        /// Get the current information model for this document
        /// </summary>
        private VersioningModel m_oCurrentModelVersion = new VersioningModel();

        /// <summary>
        /// Get all the documents versions.
        /// </summary>
        private IEnumerable<VersioningItem> m_oAllVersionDocument = new List<VersioningItem>();

        /// <summary>
        /// 
        /// </summary>
        private bool m_bIsVersionable;


        #region Historic
        /// <summary>
        /// NOT ALREADY IN USE
        /// </summary>
        private List<ActionHistoric> m_oDocumentActionHistoric = new List<ActionHistoric>();

        /// <summary>
        /// NOT ALREADY IN USE
        /// </summary>
        private List<ActionHistoric> m_oImageActionHistoric = new List<ActionHistoric>();
        #endregion

        #endregion

        #region Properties

        /// <summary>
        /// Database identifier (rd_no)
        /// </summary>
        [DataMember]
        public long Identifier
        {
            get { return m_lIdentifier; }
            set { m_lIdentifier = value; }
        }

        /// <summary>
        /// Folder ID (****)
        /// </summary>
        [DataMember]
        public long FolderId
        {
            get { return m_lFolderId; }
            set { m_lFolderId = value; }
        }

        /// <summary>
        /// Application Identifier (rd_iddoc)
        /// </summary>
        [DataMember]
        public string FolderIdentifier
        {
            get { return m_szFolderIdentifier; }
            set { m_szFolderIdentifier = value; }
        }

        /// <summary>
        /// Image name identifier (rd_idmage)
        /// </summary>
        [DataMember]
        public string ImageName
        {
            get { return m_szImageName; }
            set { m_szImageName = value; }
        }

        /// <summary>
        /// Rank of item that this document is associate (rd_item)
        /// </summary>
        [DataMember]
        public string ItemRank
        {
            get { return m_szItemRank; }
            set { m_szItemRank = value; }
        }

        /// <summary>
        /// Relative path where to find this document image (rd_path)
        /// </summary>
        [DataMember]
        public string RelativePath
        {
            get { return m_szRelativePath; }
            set { m_szRelativePath = value; }
        }

        /// <summary>
        /// Folder type identifier (rd_td)
        /// </summary>
        [DataMember]
        public long FolderTypeIdentifier
        {
            get { return m_lFolderTypeIdentifier; }
            set { m_lFolderTypeIdentifier = value; }
        }

        /// <summary>
        /// Rank of this document in the folder (rd_rang)
        /// </summary>
        [DataMember]
        public int RankInFolder
        {
            get { return m_iRankInFolder; }
            set { m_iRankInFolder = value; }
        }

        /// <summary>
        /// Note associated to this document (rd_note)
        /// </summary>
        [DataMember]
        public string Note
        {
            get { return m_szNote; }
            set { m_szNote = value; }
        }

        /// <summary>
        /// Indicate whether the document is deleted (rd_suppr)
        /// </summary>
        [DataMember]
        public bool Deleted
        {
            get { return m_bDeleted; }
            set { m_bDeleted = value; }
        }

        /// <summary>
        /// Indicate the status in the process of fullText (not used alrerady) (rd_fulltext)
        /// </summary>
        [DataMember]
        public string FullText
        {
            get { return m_szFullText; }
            set { m_szFullText = value; }
        }

        /// <summary>
        /// Indicate the Author of this document (not valued) (rd_auteur)
        /// </summary>
        [DataMember]
        public string Author
        {
            get { return m_szAuthor; }
            set { m_szAuthor = value; }
        }

        /// <summary>
        /// Indicate the credit for the Image's Author (rd_credit_photo)
        /// </summary>
        [DataMember]
        public string PhotoCredit
        {
            get { return m_szPhotoCredit; }
            set { m_szPhotoCredit = value; }
        }

        /// <summary>
        /// Indicate the courrier id of ERP Genesis (rd_id_courrier_erp)
        /// </summary>
        [DataMember]
        public string IdCourrierErp
        {
            get { return m_szIdCourrierErp; }
            set { m_szIdCourrierErp = value; }
        }

        /// <summary>
        /// Indicate the Mesure id of ERP Genesis (rd_mesure_erp)
        /// </summary>
        [DataMember]
        public string MesureErp
        {
            get { return m_szMesureErp; }
            set { m_szMesureErp = value; }
        }
       

        /// <summary>
        /// Indicate the duration of video (not valued) (rd_duree_video)
        /// </summary>
        [DataMember]
        public string VideoLength
        {
            get { return m_szVideoLength; }
            set { m_szVideoLength = value; }
        }

        /// <summary>
        /// Publication Date of image (rd_publie_le)
        /// </summary>
        [DataMember]
        public DateTime PublicationDate
        {
            get { return m_oPublicationDate; }
            set { m_oPublicationDate = value; }
        }

        /// <summary>
        /// Modification Date of image (rd_datemod)
        /// </summary>
        [DataMember]
        public DateTime ModificationDate
        {
            get { return m_oModificationDate; }
            set { m_oModificationDate = value; }
        }

        /// <summary>
        /// Identifier of folder (friendly human) (rd_id_dossier)
        /// </summary>
        [DataMember]
        public int FolderHumanIdentifier
        {
            get { return m_iFolderHumanIdentifier; }
            set { m_iFolderHumanIdentifier = value; }
        }

        /// <summary>
        /// Identifier of image (friendly human) (rd_id)
        /// </summary>
        [DataMember]
        public int ImageHumanIdentifier
        {
            get { return m_iImageHumanIdentifier; }
            set { m_iImageHumanIdentifier = value; }
        }

        /// <summary>
        /// Original name file (rd_fichier_origine)
        /// </summary>
        [DataMember]
        public string OriginalNameFile
        {
            get { return m_szOriginalNameFile; }
            set { m_szOriginalNameFile = value; }
        }

        [DataMember]
        public WorkflowState WorkflowState { get; set; }

        /// <summary>
        /// Folder where the document is stored (only root of path) (rd_pathmultivol)
        /// </summary>
        [DataMember]
        public string MultiVolumePath
        {
            get { return m_szMultiVolumePath; }
            set { m_szMultiVolumePath = value; }
        }

        /// <summary>
        /// Indicate if a thumbnail exist (rd_imagette)
        /// </summary>
        [DataMember]
        public bool Thumbnail
        {
            get { return m_bThumbnail; }
            set { m_bThumbnail = value; }
        }

        /// <summary>
        /// Return the Original file extension
        /// </summary>
        [DataMember]
        public string OriginalNameFileExtension{
            get
            {
                string l_oExt = string.Empty;
                if (this.OriginalNameFile != null && !string.Empty.Equals(this.OriginalNameFile))
                {
                    string[] l_oFilenameSplit = this.OriginalNameFile.Split('.');
                    l_oExt = l_oFilenameSplit.Last();
                }
                return l_oExt; 
            }
            set { }
        }

        /// <summary>
        /// Creation Date of image (rd_datecreation)
        /// </summary>
        [DataMember]
        public DateTime CreationDate
        {
            get { return m_oCreationDate; }
            set { m_oCreationDate = value; }
        }

        /// <summary>
        /// Folder relationship
        /// </summary>
        [Association("Folder", "ForeignKey", "Key")]
        [Include]
        [DataMember]
        public Folder Folder
        {
            get { return m_oFolder; }
            set
            {
                if (value != null)
                {
                    ForeignKey = value.Key;
                    m_oFolder = value;
                }
            }
        }

        /// <summary>
        /// Get all versions
        /// </summary>
        [Association("AllVersion", "Key", "ForeignKey")]
        [Include]
        [DataMember]
        public IEnumerable<VersioningItem> AllVersion
        {
            get { return m_oAllVersionDocument; }
            set
            {
                if (value != null)
                {
                    m_oAllVersionDocument = value;
                    ItemEntity.SetForeignKey(m_oAllVersionDocument, Key);
                }
            }
        }

        /// <summary>
        /// Get or set the model version
        /// </summary>
        [Association("CurrentModelVersion", "ForeignKey1", "Key")]
        [Include]
        [DataMember]
        public VersioningModel CurrentModelVersion
        {
            get { return m_oCurrentModelVersion; }
            set
            {
                if (value != null)
                {
                    m_oCurrentModelVersion = value;
                    ForeignKey1 = m_oCurrentModelVersion.Key;
                }
            }
        }

        /// <summary>
        /// Indicate if the document is versionable
        /// </summary>
        [DataMember]
        public bool IsVersionable
        {
            get
            {
                return m_bIsVersionable;
            }
            set
            {
                m_bIsVersionable = value;
            }
        }


        #region Historic
        /// <summary>
        /// NOT ALREADY IN USE
        /// </summary>
        public List<ActionHistoric> DocumentActionHistoricList
        {
            get { return m_oDocumentActionHistoric; }
            set { m_oDocumentActionHistoric = value; }
        }

        /// <summary>
        /// NOT ALREADY IN USE
        /// </summary>
        public List<ActionHistoric> ImageActionHistoricList
        {
            get { return m_oImageActionHistoric; }
            set { m_oImageActionHistoric = value; }
        }
        #endregion

        #endregion

        #region Init/Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public Document()
        {
            m_oCurrentModelVersion = new VersioningModel();
            m_bIsVersionable = false;
            Author = string.Empty;
            PhotoCredit = string.Empty;
            Note = string.Empty;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Document"/> class for error usages
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="applicationException">The application exception.</param>
        public Document(string errorMessage, Exception applicationException = null)
            : base(errorMessage, applicationException)
        {
        }
        #endregion

        #region ToString
        /// <summary>
        /// Extend default description of class
        /// </summary>
        /// <returns>description of instance</returns>
        public override string ToString()
        {
            StringBuilder parameters = new StringBuilder("Identifier<", 4096);
            parameters.Append(Identifier); parameters.Append("> ");
            parameters.Append("FolderIdentifier<"); parameters.Append(FolderIdentifier); parameters.Append("> ");
            parameters.Append("ImageName<"); parameters.Append(ImageName); parameters.Append("> ");
            parameters.Append("ItemRank<"); parameters.Append(ItemRank); parameters.Append("> ");
            parameters.Append("RelativePath<"); parameters.Append(RelativePath); parameters.Append("> ");

            return parameters.ToString();
        }

        #endregion

        public override object Clone()
        {
            throw new NotImplementedException();
        }
    }


    /// <summary>
    /// 
    /// </summary>
    public abstract class DocumentDer : Document
    {
        #region Abstract method
        public abstract void SetItemList();
        #endregion
    }


}
